package com.jn.rxjava.views;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
import com.jn.rxjava.R;
import com.jn.rxjava.service.ApiService;
import com.jn.rxjava.service.WeatherResponse;
import com.jn.rxjava.adapter.RetrofitClient;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class WeatherDetailActivity extends AppCompatActivity {
    private TextView textViewWeatherInfo;
    private ImageView imageViewWeather;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather_detail);

        textViewWeatherInfo = findViewById(R.id.textViewWeatherInfo);
        imageViewWeather = findViewById(R.id.imageViewWeather);

        String cityName = getIntent().getStringExtra("CITY_NAME");
        if (cityName != null) {
            fetchWeatherData(cityName);
        }
    }


    private void fetchWeatherData(String cityName) {
        ApiService apiService = RetrofitClient.getRetrofitInstance().create(ApiService.class);
        apiService.getWeather(cityName)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::displayWeatherData,
                        throwable -> textViewWeatherInfo.setText("获取天气失败: " + throwable.getMessage()));
    }

    private void displayWeatherData(WeatherResponse response) {
        if (response.getCode() == 200) { // 确保响应是成功的
            StringBuilder weatherInfo = new StringBuilder();
            weatherInfo.append("城市: ").append(response.getResult().getCityName()).append("\n")
                    .append("当前天气: ").append(response.getResult().getCurrentCondition()).append("\n")
                    .append("当前温度: ").append(response.getResult().getCurrentTemperature()).append("°C\n")
                    .append("更新时间: ").append(response.getResult().getCurrentTime()).append("\n")
                    .append("提示: ").append(response.getResult().getTips()).append("\n\n")
                    .append("未来天气预测: \n");

            for (WeatherResponse.Forecast forecast : response.getResult().getForecastList()) {
                weatherInfo.append(forecast.getDate()).append(": ")
                        .append(forecast.getCondition()).append(", 高温: ")
                        .append(forecast.getHighTemperature()).append("°C, 低温: ")
                        .append(forecast.getLowTemperature()).append("°C, ")
                        .append("风向: ").append(forecast.getWindDirection())
                        .append(", 风速: ").append(forecast.getWindLevel()).append("\n");



            }

            textViewWeatherInfo.setText(weatherInfo.toString());

            if (!response.getResult().getForecastList().isEmpty()) {
                String imageUrl = response.getResult().getForecastList().get(0).getWeatherIconId();
                Glide.with(this).load(imageUrl).into(imageViewWeather);
            }
        } else {
            textViewWeatherInfo.setText("获取天气失败: 返回代码 " + response.getCode());
        }
    }
}
